package com.dream.qudouapp.activity;

import android.app.Activity;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;

import com.dream.qudouapp.fragment.HomeFragment;
import com.example.qudouapp.R;

/**
 * 对底部的一些操作
 * 
 * @author Administrator
 * 
 */
public class BaseBottom implements OnClickListener {

	private Fragment[] fragment = {new HomeFragment()};

	private FragmentActivity activity;
	private LinearLayout lineahome, lineahospitol, lineamyinfo, lineamore;

	/**
	 * 构造函数
	 * 
	 * @param activity
	 */
	public BaseBottom(FragmentActivity activity) {
		this.activity = activity;
		getViewsBottom(activity);
	}

	/**
	 * 获取组件
	 * 
	 * @param view
	 */
	private void getViewsBottom(Activity view) {
		lineahome = (LinearLayout) view
				.findViewById(R.id.bottom_homelinealayout);
		lineahospitol = (LinearLayout) view
				.findViewById(R.id.bottom_hospitolinealayout);
		lineamyinfo = (LinearLayout) view
				.findViewById(R.id.bottom_myinfolinealayout);
		lineamore = (LinearLayout) view
				.findViewById(R.id.bottom_morelinealayout);
		setBottomClick();
	}

	/**
	 * bottom点击事件
	 * 
	 */
	public void setBottomClick() {
		lineahome.setOnClickListener(this);
		lineahospitol.setOnClickListener(this);
		lineamyinfo.setOnClickListener(this);
		lineamore.setOnClickListener(this);

	}

	@Override
	public void onClick(View v) {
		if (v == lineahome) {
			intentFragment(fragment[0]);
		} else if (v == lineahospitol) {
			intentFragment(fragment[1]);
		} else if (v == lineamyinfo) {
			intentFragment(fragment[2]);
		} else if (v == lineamore) {
			intentFragment(fragment[3]);
		}
	}

	/**
	 * fragment替换
	 * 
	 * @param fragment
	 */
	private void intentFragment(Fragment fragment) {
		FragmentTransaction transaction = activity.getSupportFragmentManager()
				.beginTransaction();
		fragment.setRetainInstance(true);
		transaction.replace(R.id.activity_fragment, fragment);
		transaction.commit();
	}

}
